home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1995 November / EnigmA AMIGA RUN 02 (1995)(G.R. Edizioni)(IT)[!][issue 1995-11][Skylink CD].iso / earcd / unix / mp14tar.z / mp14tar / mpack / getopt.c < prev    next >
C/C++ Source or Header  |  1994-06-01  |  3KB  |  107 lines

  1. /* (C) Copyright 1993 by John G. Myers
  2.  * All Rights Reserved.
  3.  *
  4.  * Permission to use, copy, modify, distribute, and sell this software
  5.  * and its documentation for any purpose is hereby granted without
  6.  * fee, provided that the above copyright notice appear in all copies
  7.  * and that both that copyright notice and this permission notice
  8.  * appear in supporting documentation, and that the name of John G.
  9.  * Myers not be used in advertising or publicity pertaining to
  10.  * distribution of the software without specific, written prior
  11.  * permission.  John G. Myers makes no representations about the
  12.  * suitability of this software for any purpose.  It is provided "as
  13.  * is" without express or implied warranty.
  14.  *
  15.  * JOHN G. MYERS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
  16.  * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  17.  * FITNESS, IN NO EVENT SHALL JOHN G. MYERS BE LIABLE FOR ANY SPECIAL,
  18.  * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
  19.  * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  20.  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
  21.  * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  22.  */
  23. #include <stdio.h>
  24.  
  25. #define ERR(s, c)                    \
  26.     if (opterr) {                    \
  27.     char buff[2];                    \
  28.     buff[0] = c; buff[1] = '\n';            \
  29.     (void)write(2, av[0], strlen(av[0]));        \
  30.     (void)write(2, s, strlen(s));            \
  31.     (void)write(2, buff, 2);            \
  32.     }
  33.  
  34.  
  35. int    opterr = 1;
  36. int    optind = 1;
  37. int    optopt;
  38. char    *optarg;
  39.  
  40.  
  41. /*
  42. **  Return options and their values from the command line.
  43. **  This comes from the AT&T public-domain getopt published in mod.sources
  44. **  (i.e., comp.sources.unix before the great Usenet renaming).
  45. */
  46. int
  47. getopt(ac, av, opts)
  48.     int        ac;
  49.     char    *av[];
  50.     char    *opts;
  51. {
  52.     extern char    *strchr();
  53.     static int    i = 1;
  54.     char    *p;
  55.  
  56.     /* Move to next value from argv? */
  57.     if (i == 1) {
  58.     if (optind >= ac ||
  59. #ifdef __MSDOS__
  60.         (av[optind][0] != '-' && av[optind][0] != '/')
  61. #else
  62.         av[optind][0] != '-'
  63. #endif
  64.         || av[optind][1] == '\0')
  65.         return EOF;
  66.     if (strcmp(av[optind], "--") == 0) {
  67.         optind++;
  68.         return EOF;
  69.     }
  70.     }
  71.  
  72.     /* Get next option character. */
  73.     if ((optopt = av[optind][i]) == ':'
  74.      || (p = strchr(opts,  optopt)) == NULL) {
  75.     ERR(": illegal option -- ", optopt);
  76.     if (av[optind][++i] == '\0') {
  77.         optind++;
  78.         i = 1;
  79.     }
  80.     return '?';
  81.     }
  82.  
  83.     /* Snarf argument? */
  84.     if (*++p == ':') {
  85.     if (av[optind][i + 1] != '\0')
  86.         optarg = &av[optind++][i + 1];
  87.     else {
  88.         if (++optind >= ac) {
  89.         ERR(": option requires an argument -- ", optopt);
  90.         i = 1;
  91.         return '?';
  92.         }
  93.         optarg = av[optind++];
  94.     }
  95.     i = 1;
  96.     }
  97.     else {
  98.     if (av[optind][++i] == '\0') {
  99.         i = 1;
  100.         optind++;
  101.     }
  102.     optarg = NULL;
  103.     }
  104.  
  105.     return optopt;
  106. }
  107.